package com.guoguo.controller;

import com.guoguo.entity.UserExt;
import com.guoguo.entity.Users;
import com.guoguo.service.IUserExtService;
import com.guoguo.service.IUsersService;
import com.guoguo.utils.UUIDUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * Created by Administrator on 2017/8/8 0008.
 */
@Controller
@RequestMapping("/authority")
public class LoginController {

    @Resource
    private IUsersService usersService;
    @Resource
    private IUserExtService iUserExtService;

    /**
     * 后台登录
     *
     * @param request
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String Login(HttpServletRequest request, @RequestParam String userName, @RequestParam String password) {
        try {
            //如果是中文，则转一下编码
            userName = new String(userName.getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Users users = usersService.findUser(userName, password);
        HttpSession session = request.getSession();
        if (users != null) {
            session.setAttribute("user", users);
        }
        return "redirect:/index";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return "redirect:/index";
    }


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@RequestParam String userName, @RequestParam String password, @RequestParam String nickName) {
        try {
            //如果是中文，则转一下编码
            userName = new String(userName.getBytes("iso-8859-1"), "utf-8");
            nickName = new String(nickName.getBytes("iso-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int userObj = usersService.count(userName);
        if (userObj == 0) {
            Users user = new Users();
            UserExt userExt = new UserExt();
            user.setUserId(UUIDUtils.getUUID());
            user.setUsername(userName);
            user.setUserpwd(password);
            user.setPhone(userName);
            user.setNickname(nickName);
            user.setPhoneType(10);
            user.setAppId(0);
            int users = usersService.insertUsers(user);
            userExt.setUserId(user.getUserId());
            userExt.setEmail(null);
            userExt.setPhone(user.getPhone());
            Date now = new Date();
            userExt.setRegistered(now);
            userExt.setCreateTime(now);
            userExt.setSex(false);
            userExt.setIcon(null);
            userExt.setBuyCount(0);
            //后台运营用户
            userExt.setBuySurplus(0);
            userExt.setIsMember(false);
            userExt.setMemType(null);
            userExt.setMoney(null);
            userExt.setPoint(null);

            iUserExtService.insertUserExt(userExt);
        }
        return "redirect:/index";
    }
}
